package pw.archipelag.loaderGame {
	import base.loader.LoaderOneSwf;
	import pw.archipelag.loaderGame.bg.Bg;
	import pw.archipelag.loaderGame.interfaceGame.IGame;
	import flash.display.Sprite;
	import pw.archipelag.loaderGame.panel.PanelMini;
	import pw.archipelag.loaderGame.panel.PanelStartEndGame;
	
	import base.app.InitUtils;
	import com.greensock.TweenNano;
	import com.greensock.easing.Linear;
	import base.loader.show.LoaderProgress;
	import pw.archipelag.loaderGame.loaderShow.Progress;
	import base.loader.URLLoaderExt;
	import pw.archipelag.loaderGame.interfaceGame.DescriptionPlayer;
	import base.dspObj.DoOperations;
	import pw.archipelag.loaderGame.interfaceGame.EventLoaderGame;
	import flash.display.DisplayObject;
	import base.btn.BtnHitEvent;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinPanelSet;
	import flash.events.Event;

	
	[SWF(backgroundColor = "0x333333", frameRate = "35", width = "750", height = "650")]
	
	public class LoaderGame extends LoaderOneSwf {
		
		private var bg: Bg;
		private var game: IGame;
		private var containerPanels: Sprite;
		private var panelMini: PanelMini;
		private var panelStartEndGame: PanelStartEndGame;
		
		public function LoaderGame(): void {
			Config.PATH_LOADER_GAME = root.loaderInfo.url.substring(0, root.loaderInfo.url.lastIndexOf("/") + 1);
			Config.URL_SERVICE = ((root.loaderInfo.parameters.pathService) ? String(root.loaderInfo.parameters.pathService) : "") + "/CommunicationService.svc/";
			Config.ID_SESSION = (root.loaderInfo.parameters.idSession) ? String(root.loaderInfo.parameters.idSession) : "";
			Config.URL_GAME = (root.loaderInfo.parameters.urlGame) ? String(root.loaderInfo.parameters.urlGame) : "TemplateGameDefault.swf";
			this.createBg();
			super(Config.URL_GAME);
		}
		
		private function createBg(): void {
			this.bg = new Bg();
			this.addChild(this.bg);
			TweenNano.from(this, LoaderOneSwf.COUNT_FRAMES_SHOW_SWF, {alpha: 0, useFrames: true, ease: Linear.easeNone})
		}
		
		override protected function createProgress(): LoaderProgress {
			var progress: Progress = new Progress();
			return progress;
		}
		
		override protected function hideLoaderShowAndShowSwf(): void {
			var urlLoaderExt: URLLoaderExt = new URLLoaderExt();
			urlLoaderExt.sendAndLoad(Config.URL_SERVICE + "getprofile/" + Config.ID_SESSION, [], [this.callbackGetProfile, this], 1, 60000, false);
		}

		private function callbackGetProfile(isLoaded: Boolean, nodeProfile: String, ...args): void {
			if (isLoaded) {
				nodeProfile = nodeProfile.replace(new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi"), "");
				Config.DESCRIPTION_PLAYER = new DescriptionPlayer(URLLoaderExt.parseXMLToObject(new XML(nodeProfile)));
			}
			DoOperations.hideShow(this.bg, 0, LoaderOneSwf.COUNT_FRAMES_SHOW_HIDE, this.removeBg);
			super.hideLoaderShowAndShowSwf();
		}

		private function removeBg(): void {
			this.bg.destroy();
			this.removeChild(this.bg);
			this.bg = null;
		}
		
		override protected function showSwf(isTweenShow: Boolean): void {
			super.showSwf(true);
			this.game = IGame(this.swf.content);
			this.stage.addEventListener(Event.RESIZE, this.changeGameDimensionOnStageResize);
			this.stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function changeGameDimensionOnStageResize(e: Event): void {
			var dspObjGame: DisplayObject = DisplayObject(this.game);
			var scaleDspObjGame: Number = Math.min(this.stage.stageWidth / dspObjGame.loaderInfo.width, this.stage.stageHeight / dspObjGame.loaderInfo.height);
			dspObjGame.scaleX = dspObjGame.scaleY = scaleDspObjGame;
			dspObjGame.x = (this.stage.stageWidth - dspObjGame.loaderInfo.width * dspObjGame.scaleX) / 2;
			dspObjGame.y = (this.stage.stageHeight - dspObjGame.loaderInfo.height * dspObjGame.scaleY) / 2;
		}
		
		override protected function initSwf(): void {
			var isNotAddPanelStartEndGame: Boolean = this.initGame();
			this.addPanelMini();
			if (!isNotAddPanelStartEndGame) this.game.dispatchEvent(new EventLoaderGame(EventLoaderGame.RESTART_GAME));
			else this.panelMini.hideShow(0);
		}
		
		private function initGame(): Boolean {
			this.game.addEventListener(EventLoaderGame.SAVE_SCORE, this.onSaveScore);
			this.game.addEventListener(EventLoaderGame.RESTART_GAME, this.onRestartGame);
			this.containerPanels = new Sprite();
			this.addChild(this.containerPanels);
			return Boolean(this.game.init(Config.DESCRIPTION_PLAYER));
		}
		
		//////panel mini
		
		private function addPanelMini(): void {
			this.panelMini = new PanelMini();
			this.panelMini.btnHelp.addEventListener(BtnHitEvent.CLICKED, this.addPanelStartEndGameHelp);
			this.panelMini.btnScores.addEventListener(BtnHitEvent.CLICKED, this.addPanelStartEndGameRanking);
			this.containerPanels.addChild(this.panelMini);
			var skinPanelSet: SkinPanelSet = this.game.skinPanelSet || new SkinPanelSet();
			this.panelMini.draw(skinPanelSet.skinPanelMini, skinPanelSet.skinBg, skinPanelSet.skinBtnPanel);
		}
		
		//////panel start-end game, btnHelp, btnScores
		
		private function addRemovePanelStartEndGameAndRemoveAddPanelMini(isRemoveAddPanelStartEndGame: uint, isShowHelpRanking: int = -1, isFromStartEndGameFromBtnHelpScore: int = -1, scorePoints: int = -1, scorePercent: int = -1): void {
			//trace(isRemoveAddPanelStartEndGame, isShowHelpRanking, isFromStartEndGameFromBtnHelpScore)
			this.addRemovePanelStartEndGame(isRemoveAddPanelStartEndGame, isShowHelpRanking, isFromStartEndGameFromBtnHelpScore, scorePoints, scorePercent);
			if (isFromStartEndGameFromBtnHelpScore == 1)
				this.game.isPauseContinue = (1 - isRemoveAddPanelStartEndGame);
			this.panelMini.hideShow(1 - isRemoveAddPanelStartEndGame);
		}
		
		private function addRemovePanelStartEndGame(isRemoveAdd: uint, isShowHelpRanking: int = -1, isFromStartEndGameFromBtnHelpScore: int = -1, scorePoints: int = -1, scorePercent: int = -1): void {
			if (isRemoveAdd == 0) {
				this.panelStartEndGame.removeEventListener(Event.CLOSE, [this.removePanelStartEndGameAndStartGame, this.removePanelStartEndGameAndContinueGame][isFromStartEndGameFromBtnHelpScore]);
				this.containerPanels.removeChild(this.panelStartEndGame);
				this.panelStartEndGame = null;
			} else if (isRemoveAdd == 1) {
				if (this.panelStartEndGame != null) {
					this.panelStartEndGame.removeEventListener(Event.CLOSE, this.removePanelStartEndGameAndStartGame);
					this.panelStartEndGame.removeEventListener(Event.CLOSE, this.removePanelStartEndGameAndContinueGame);
					DoOperations.hideShow(this.panelStartEndGame, 0, 10, this.panelStartEndGame.destroy);
				}
				this.panelStartEndGame = new PanelStartEndGame(isShowHelpRanking, this.game.nameGame, this.game.classMcHelp, scorePoints, scorePercent);
				this.panelStartEndGame.addEventListener(Event.CLOSE, [this.removePanelStartEndGameAndStartGame, this.removePanelStartEndGameAndContinueGame][isFromStartEndGameFromBtnHelpScore]);
				this.containerPanels.addChild(this.panelStartEndGame);
				var skinPanelSet: SkinPanelSet = this.game.skinPanelSet || new SkinPanelSet();
				this.panelStartEndGame.draw(skinPanelSet.dimensionPanelStartEndGame, skinPanelSet.skinBg, skinPanelSet.skinTfLabelTitles, skinPanelSet.skinBtnPanel, skinPanelSet.skinScores, skinPanelSet.skinHelp);
			}
		}
		
		private function addPanelStartEndGameHelp(e: BtnHitEvent): void {
			this.addRemovePanelStartEndGameAndRemoveAddPanelMini(1, 0, 1);
		}
		
		private function addPanelStartEndGameRanking(e: BtnHitEvent): void {
			this.addRemovePanelStartEndGameAndRemoveAddPanelMini(1, 1, 1);
		}
		
		private function removePanelStartEndGameAndContinueGame(e: Event): void {
			this.addRemovePanelStartEndGameAndRemoveAddPanelMini(0, -1, 1);
		}
		
		private function removePanelStartEndGameAndStartGame(e: Event): void {
			this.addRemovePanelStartEndGameAndRemoveAddPanelMini(0, -1, 0);
			this.game.start();
		}
		
		//interface
		
		public function onSaveScore(e: EventLoaderGame): void {
			this.addRemovePanelStartEndGameAndRemoveAddPanelMini(1, 1, 0, e.data.scorePoints, e.data.scorePercent);
		}
		
		private function onRestartGame(e: EventLoaderGame): void {
			this.addRemovePanelStartEndGameAndRemoveAddPanelMini(1, 0, 0);
		}
		
	}
	
}